package com.czn.controller.home;

import com.czn.entity.User;
import com.czn.service.IUserService;
import com.czn.vo.ActiveUser;
import com.czn.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author ChenZeNan
 * @create 2021-04-14 17:46
 */
@Controller
@RequestMapping("/home")
public class LoginController {
    @Autowired
    IUserService userService;

    /**
     * 跳转到登录注册页面
     *
     * @return
     */
    @RequestMapping("/loginRegisterPage")
    public String loginRegisterPage() {
        return "home/login&regist";
    }

    /**
     * 进行登录验证
     *
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public ResponseResult login(@RequestBody User user, HttpSession session) {
        ResponseResult result = userService.checkUser(user, session);
        return result;
    }

    /**
     * 用户注册
     *
     * @return
     */
    @RequestMapping("/regest")
    @ResponseBody
    public ResponseResult register(@RequestBody ActiveUser activeUser) {
        ResponseResult result = userService.registUser(activeUser);
        return result;
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/home/loginRegisterPage";
    }
}
